﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title>Helper</title>
    <style type="text/css">
    html, body {
	    height: 100%;
	    overflow: auto;
    }
    body {
	    padding: 0;
	    margin: 0;
    }
    #silverlightControlHost {
	    /*height: 100%;
	    text-align:center;*/
    }
    </style>
    <script type="text/javascript" src="Silverlight.js"></script>
    <script type="text/javascript">
        function onSilverlightError(sender, args) {
            var appSource = "";
            if (sender != null && sender != 0) {
              appSource = sender.getHost().Source;
            }
            
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            if (errorType == "ImageError" || errorType == "MediaError") {
              return;
            }

            var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;

            errMsg += "Code: "+ iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {           
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " +  args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }
    </script>
    <script type="text/javascript">
        //$
        function $(query) { return document.querySelector(query); }

        //log functions
        function log() { logText(Array.map(arguments, formatLogObject).join(", "), arguments.callee.caller ? arguments.callee.caller.name ? arguments.callee.caller.name : "" : ""); } //arguments.callee.caller.toSource()
        function logText(text, source) {$("#logArea").value += text + "\n";   if (console) console.log(text);  if (_logger) _logger.Log(text, source); }
        function formatLogObject(obj) { return obj == null ? "<null>" : typeof (obj) == "object" && typeof (obj.toSource) == "function" ? obj.toSource() : typeof (obj) == "function" ? obj.name ? "<" + obj.name + ">" : obj.toSource() : obj; }

        var _comm;
        var _logger;
        function onSilverlightLoad(s,e) {
            var cont = document.getElementById("helper").Content;
            _logger = cont.Logger;
            log("Logger is active: " + _logger.IsActive);
            _comm = cont.Communicator;
            log("Communicator is active: " + _comm.IsActive);
            log("Communicator is master: " + _comm.IsMaster);
            log("Communicator receiver name: " + _comm.ReceiverName);
            log("Communicator sender names: " + _comm.SenderNames);
            _comm.MessageReceived=messageReceived;
            _comm.SendCompleted=sendCompleted;
            _comm.ScriptablePropertyChanged = propertyChanged;
        }

        function clearLogs() {
            if (_logger) _logger.ClearLogs();
        }

        function propertyChanged(sender, e) {
            var val = "";
            try { val = e.Value; }
            catch (ex) { val = "[" + ex.message + "]"; }
            log("propertyChanged: _comm." + e.PropertyName + "=" + val);
            if (e.PropertyName == "SenderNames") $("#senders").value = val;
        }

        function sendCommMessage(message) {
            if (_comm) _comm.Send(message);
        }

        function messageReceived(s, e) {
            var text=e.Sender + ": " + e.Message;
            log(text);
            e.Response = prompt(text);
            if (e.Response) log(e.Receiver + ": " + e.Response);
        }

        function sendCompleted(s, e) {
            log(e.Sender + ": " + e.Message);
            if (e.Response) log(e.Receiver + ": " + e.Response);
        }
    </script>
</head>
<body>
    <div id="silverlightControlHost">
        <object id="helper" data="data:application/x-silverlight-2," type="application/x-silverlight-2" height="50px" width="100px">
		  <param name="source" value="ClientBin/Helper.xap"/>
		  <param name="onError" value="onSilverlightError" />
          <param name="onLoad" value="onSilverlightLoad" />
		  <param name="background" value="white" />
		  <param name="minRuntimeVersion" value="5.1.10411.0" />
		  <param name="autoUpgrade" value="true" />
          <param name="enableHtmlAccess" value="true" />
          <param name="initParams" value="LogFilePathFormat={0:yyyy_MM}/{0:yyyy_MM_dd}_Test.txt,ReceiverName=Test" />
		  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.1.10411.0" style="text-decoration:none">
 			  <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
		  </a>
	    </object>
    </div>
    <div>
        <input type="text" value="Log Message" onkeyup="if (event.keyCode==13) {log(this.value);this.value='';}" /><br />
        <input type="text" value="Test" onkeyup="if (event.keyCode==13) {_comm.ReceiverName=this.value;}" /><br />
        <input type="text" value="Message" onkeyup="if (event.keyCode==13 && this.value!='') {_comm.Send(this.value);this.value='';}" /><br />
        <input type="text" id="senders" readonly="readonly" /><br />
        <textarea id="logArea" rows="20" cols="100"></textarea>
    </div>
</body>
</html>
